<template>
  <div class="replace-record">
    <el-row class="btn-operation">
      <el-col>
        <el-button type="primary" size="small" @click="openDialogHandle('Add', null)"> 新增 </el-button>
      </el-col>
    </el-row>
    <el-table :data="tableData" border>
      <el-table-column label="序号" type="index" width="50px"></el-table-column>
      <el-table-column label="更换时间" prop="replaceTime" width="180px"></el-table-column>
      <el-table-column label="操作人" prop="updateByName" width="150px"></el-table-column>
      <el-table-column label="易损件名称" prop="parts" show-overflow-tooltip>
        <template slot-scope="scope">
          <span>{{ (partsOptions.find(item => item.value == scope.row.parts) || {}).label || scope.row.parts }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
      <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="openDialogHandle('Edit', scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        :page-size="pageVO.pageSize"
        :current-page.sync="pageVO.pageNum"
        :total="pageVO.total"
        :layout="pageOptions.layout"
        :page-sizes="pageOptions.pageSizes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <replaceRecordDialog :partsOptions="partsOptions" ref="dialogRef" @submit="getTableData(1)" />
  </div>
</template>

<script>
import pageQuery from '../../../mixins/pageQuery';
import { getChangeList, deleteChange, getDicts } from '@/api/maintenance';
import replaceRecordDialog from './replaceRecordDialog.vue';

export default {
  mixins: [pageQuery],
  components: {
    replaceRecordDialog,
  },
  data() {
    return {
      partsOptions: [],
    };
  },
  created() {
    this.getPartsOptions();
  },
  methods: {
    getPartsOptions() {
      getDicts({
        dictType: 'sys_device_parts',
      }).then(res => {
        const { data } = res.data || {};
        this.partsOptions = (data || []).map(item => {
          return {
            label: item.dictLabel,
            value: item.dictValue,
          };
        });
      });
    },
    deleteHandle(row) {
      console.log('row', row);
      this.$confirm('是否删除该条记录？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteChange({
          id: row.id,
        }).then(res => {
          if (res.success) {
            this.$message.success('删除成功');
            this.getTableData(1);
          } else {
            this.$message.error('删除失败');
          }
        });
      });
    },
    getTableData(pageNum) {
      if (pageNum) {
        this.pageVO.pageNum = pageNum;
      }
      getChangeList({
        pageNo: this.pageVO.pageNum,
        pageSize: this.pageVO.pageSize,
        projectId: this.$store.getters.projectId,
      }).then(res => {
        const { data, total } = res.data || {};
        this.tableData = data || [];
        this.pageVO.total = total;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.replace-record {
  .btn-operation {
    margin-bottom: 15px;
  }
}
</style>
